package com.sphd.beautysalon.activitys;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.Toolbar;
import android.text.InputType;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.sphd.beautysalon.activitys.vip.DuiJifenActivity;
import com.sphd.beautysalon.info.LoginInfo;
import com.sphd.beautysalon.interfaces.IntegralCallBack;
import com.sphd.beautysalon.R;
import com.sphd.beautysalon.adapters.MyViewPagerAdapter;
import com.sphd.beautysalon.base.BaseActivity;
import com.sphd.beautysalon.base.RestaurantUrl;
import com.sphd.beautysalon.fragments.DhjfHistoryFragment;
import com.sphd.beautysalon.fragments.GetjfHistoryFragment;
import com.sphd.beautysalon.info.RestaurantInfo;
import com.sphd.beautysalon.info.VipInfo;
import com.sphd.beautysalon.presenter.VipJifenShowPresenterImpl;
import com.sphd.beautysalon.utils.Constant;
import com.sphd.beautysalon.view.VipJifenShowView;
import com.wzh.frame.utils.StringUtils;

import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 积分详情
 */
public class VipJifenShowActivity extends BaseActivity implements IntegralCallBack, VipJifenShowView {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.surplus_jifen)
    TextView surplusJifen;
    @BindView(R.id.add_jifen)
    Button addJifen;
    @BindView(R.id.duihaun_jifen)
    Button duihaunJifen;
    @BindView(R.id.jifen_history_tablauout)
    TabLayout jifenHistoryTablauout;
    @BindView(R.id.jifen_history_viewpager)
    ViewPager jifenHistoryViewpager;
    Intent intent;
    Bundle bundle;
    VipInfo vipInfo;
    RestaurantInfo restaurantInfo;
    MyViewPagerAdapter myViewPagerAdapter;
    List<Fragment> fragmentList;
    String[] title = {"兑换记录", "获取记录"};
    private DhjfHistoryFragment dhjfHistoryFragment;
    private GetjfHistoryFragment getjfHistoryFragment;
    private LoginInfo loginInfo;
    private static final String TAG = "VipJifenShowActivity";
    VipJifenShowPresenterImpl vipJifenShowPresenter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_vip_jifen_show);
        ButterKnife.bind(this);
        initView();
        initEvent();
    }

    private void initEvent() {
        toolbar.setNavigationOnClickListener(view -> {
            setResult(0);
            finish();
        });
    }

    private void initView() {
        intent = getIntent();
        bundle = intent.getExtras();
        Log.e(TAG, bundle.getInt("a") + "");
        setTitleBar(toolbar, "积分详情");
        vipInfo = (VipInfo) bundle.getSerializable(Constant.VIPINFO);
        loginInfo = DataSupport.findLast(LoginInfo.class);
        restaurantInfo = (RestaurantInfo) bundle.getSerializable(Constant.RESTAURANTSINFO);

        vipJifenShowPresenter=new VipJifenShowPresenterImpl(this);
        fragmentList = new ArrayList<>();
        dhjfHistoryFragment = new DhjfHistoryFragment();
        getjfHistoryFragment = new GetjfHistoryFragment();
        dhjfHistoryFragment.setVal(bundle, this);
        getjfHistoryFragment.setVal(bundle, this);
        fragmentList.add(dhjfHistoryFragment);
        fragmentList.add(getjfHistoryFragment);
        initAdapter();
        if (!isCanDo()){
            addJifen.setVisibility(View.GONE);
            duihaunJifen.setVisibility(View.GONE);
        }


    }

    private void initAdapter() {
        myViewPagerAdapter = new MyViewPagerAdapter(getSupportFragmentManager(), fragmentList, title);
        jifenHistoryViewpager.setAdapter(myViewPagerAdapter);
        jifenHistoryTablauout.setupWithViewPager(jifenHistoryViewpager);

    }

    @OnClick({R.id.add_jifen, R.id.duihaun_jifen})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.add_jifen:
                showEditDialog("积分赠送数量", 0);
                break;
            case R.id.duihaun_jifen:
                intent.setClass(context, DuiJifenActivity.class);
                intent.putExtra("vip", bundle);
                startActivityForResult(intent, 0);
                break;
        }
    }

    private boolean isCanDo() {
        if (!isReciver(loginInfo)) {
            if (isSuperManager(loginInfo)) {
                if (!isCg_has_storemanager(restaurantInfo)) {
                    return true;
                }else {
                    return false;
                }
            } else {
                return false;
            }
        } else {
            return true;
        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 0)
            dhjfHistoryFragment.onRefresh();
    }

    @Override
    public void returnIntegral(int integral) {
        surplusJifen.setText(String.valueOf(integral));
    }

    private void showEditDialog(String title, int type) {
        LayoutInflater li = LayoutInflater.from(context);
        View promptsView = li.inflate(R.layout.layout_edittextdialog, null);

        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                context);

        alertDialogBuilder.setView(promptsView);

        final EditText userInput = (EditText) promptsView
                .findViewById(R.id.input);
        userInput.setHint("");
        userInput.setInputType(InputType.TYPE_CLASS_NUMBER);
        alertDialogBuilder
                .setCancelable(false)
                .setTitle(title)
                .setPositiveButton("确定",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {

                                if (isNumber1(userInput.getText().toString().trim())) {
                                    if (Integer.valueOf(userInput.getText().toString()) < 1000) {
                                        addEtickets(userInput.getText().toString().trim(), type);
                                    } else {
                                        toast("赠送积分过大");
                                    }
                                } else {
                                    toast("填入数字");
                                }


                            }
                        })
                .setNegativeButton("取消",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                dialog.cancel();
                            }
                        });

        // create alert dialog
        AlertDialog alertDialog = alertDialogBuilder.create();

        // show it
        alertDialog.show();
    }

    private void addEtickets(String trim, int type) {
        vipJifenShowPresenter.addData(RestaurantUrl.Httptitle +
                RestaurantUrl.ADD_INTEGRAL_EDIT+"cgid="+vipInfo.getMem_cgid()+"&memphone="+vipInfo.getMem_phone()+"&integral="+trim
        + (StringUtils.isEmpty(vipInfo.getMem_card())?"":"&memcard="+vipInfo.getMem_card()));
    }

    @Override
    public void recRes(int res) {
        if (res==1){
            toast("赠送成功");
            jifenHistoryViewpager.setCurrentItem(1);
            getjfHistoryFragment.onRefresh();
        }else {
            toast("赠送失败");
        }
    }

    @Override
    public void showLoading() {
        showLoadingDialog();
    }

    @Override
    public void dissLoading() {
        dissLoadingDialog();
    }
}
